const loaderUtils = require('loader-utils')
const { validate } = require('schema-utils')
const fs = require('fs')

function loader(source) {
  // this.cacheable(false)
  this.cacheable && this.cacheable()
  const options = loaderUtils.getOptions(this)
  const cb = this.async()
  const schema = {
    type: 'object',
    properties: {
      text: {
        type: 'string',
      },
      filename: {
        type: 'string',
      },
    },
    additionalProperties: false
  }
  console.log('loader => 111')

  validate(schema, options, {name: 'banner-loader'})

  if (options.filename) {
    // 将修改的文件，添加到监听对象
    this.addDependency(options.filename)
    console.log('loader => 111', options.filename)
    fs.readFile(options.filename, 'utf8', function (err, data) {
      cb(err, `/**${ data.replace(/\t$/, '') }**/${ source }`)
    })
  } else {
    cb(err, `/**${ options.text }**/${ source }`)
  }
  /*babel.transform(source, {
    // presets: options.presets,
    ...options,
    sourceMap: true,
    filename: this.resourcePath.split('/').pop(),
  }, function (err, result) {
    cb(err, result.code, result.map, 'babel-loader')
  })*/
}

module.exports = loader
